<?php 

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class BrandController extends Controller
{
    public function index(Request $request)
    {
    	$title = "品牌管理";

    	$data = DB::table('brand')
    	->where('name','like','%'.$request->input('keywords').'%')
         ->paginate($request->input('num',8));
    	
    	return view('admin.brand.index',['title'=>$title,'data'=>$data,'request'=>$request -> all()]);
    }
    public function edit($id)
    {

    	$title = "品牌编辑";
       
        $data =DB::table('brand') -> where('id',$id)->first();
        
        return view('admin.brand.edit',['title'=>$title,'data'=>$data]);

    	
    }
    public function add()
    {  

    	$title = "品牌添加";
    	return view('admin.brand.add',['title'=>$title]);
    }
    public function insert(Request $request)
    {   
        //laravel内置验证
        $this ->validate($request,[
            'name'=>'required',
            'logo'=>'required',
            'desc'=>'required',
            'url'=>'required',
          
           
            ],[
            'name.required'=>'品牌名称不能为空',
            'logo.required'=>'品牌logo不能为空',
            'desc.required'=>'品牌自述不能为空',
            'url.required'=>'品牌链接不能为空'
            ]);
        $data = $request -> except('_token','logo');
        //处理图片

        if($request ->hasfile('logo'))
        {
            if($request ->file('logo')->isValid())
                {
                    $suffix = $request->file('logo')->getClientOriginalExtension();
                        //生成随机文件名称
                        $filename = 'brand'.date('Y-m-d H-i-s',time()).'.'.$suffix;
                       
                        //yidoing文件
                        $request -> file('logo')->move('./uploads/brand',$filename);
                        
                        //拼接pic路径
                        $data['logo'] = '/uploads/brand/'.$filename;
                }
            }
        $res = DB::table('brand') ->insert($data);
        if($res)
            {
                return redirect('/admin/brand')->with(['info'=>'添加成功']);
            }else{
                return back()->with(['info'=>'添加失败']);
            }
    }


    public function update(Request $request )
    {   
        $id = $request ->input('id');
        //laravel内置验证
        $this ->validate($request,[
            'name'=>'required',
            'logo'=>'required',
            'desc'=>'required',
            'url'=>'required',
          
           
            ],[
            'name.required'=>'品牌名称不能为空',
            'logo.required'=>'品牌logo不能为空',
            'desc.required'=>'品牌自述不能为空',
            'url.required'=>'品牌链接不能为空'
            ]);
       
        $data = $request -> except('_token','logo','oldlogo');
        //处理图片
       
        if($request ->hasfile('logo'))
        {
            if($request ->file('logo')->isValid())
                {
                    $suffix = $request->file('logo')->getClientOriginalExtension();
                        //生成随机文件名称
                        $filename = 'brand'.date('Y-m-d H-i-s',time()).'.'.$suffix;
                        //yidoing文件
                        $request -> file('logo')->move('./uploads/brand',$filename);
                        
                        //拼接pic路径
                        $data['logo'] = '/uploads/brand/'.$filename;
                        $oldlogo = '.'.$request -> input('oldlogo');
                        
                        if(file_exists($oldlogo))
                        {
                            unlink($oldlogo);
                        }
                }
            }
        
        $res = DB::table('brand')->where('id',$id) ->update($data);


        if($res)
            {
                return redirect('/admin/brand')->with(['info'=>'添加成功']);
            }else{
                return back()->with(['info'=>'添加失败']);
            }
    }
    public function delete($id)
    {
        $res =  DB::table('brand')->where('id',$id) ->first();
        
        $logo = '.'.$res->logo;
        
        if(file_exists($logo))
                        {   
                            
                            unlink($logo);
                        }
       $res =  DB::table('brand')->where('id',$id) ->delete();
       if($res)
            {
                return redirect('/admin/brand')->with(['info'=>'删除成功']);
            }else{
                return back()->with(['info'=>'删除失败']);
            }

    }
}
